<?php

namespace app\services\product;

use app\dao\product\BrandDao;
use app\model\product\Brand;
use app\services\BaseServices;
use crmeb\exceptions\AdminException;

class BrandService extends BaseServices
{

    public function __construct(BrandDao $dao)
    {
        $this->dao = $dao;
    }

    public function getList(array $where)
    {
        $page = $where['page'] ?? 1;
        $limit = $where['limit'] ?? 15;
        $list = $this->dao->getList($where, $page, $limit);

        return [
            'list'  =>  $list->items(),
            'count' =>  $list->count()
        ];
    }

    public function all()
    {
        return Brand::where('is_del', 0)->where('status', 1)->field('id,name')->select()->toArray();
    }

    public function save(int $id, array $data)
    {

        if (!isset($data['name']) || $data['name'] == '') throw new AdminException(30002);
        if (!isset($data['logo']) || $data['logo'] == '') throw new AdminException(30003);
        if (!isset($data['description']) || $data['description'] == '') throw new AdminException(30004);
        if (!isset($data['banner']) || $data['banner'] == '') throw new AdminException(30005);

        if ($id) {
            $res = $this->dao->update($id, $data);
            if (!$res) throw new AdminException(10022);
        } else {
            $data['status'] = 1;
            $data['add_time'] = time();
            $res = $this->dao->save($data);
            if (!$res) throw new AdminException(10022);
        }

    }


    /**
     * 删除
     * @param int $id
     */
    public function del(int $id)
    {
        $res = $this->dao->update($id, ['is_del' => 1]);
        if (!$res) throw new AdminException(100008);
    }



    public function getForntInfo(int $id)
    {
        $info = $this->dao->get($id, ['id', 'banner', 'name', 'description']);
        $info['articles'] = [
            [
                'id'    =>  1,
                'title' =>  '品牌文章01',
                'image' =>  'https://v5.crmeb.net/uploads/attach/2024/02/20240202/ea0c81caf1cede3622dea113dacaa25e.png',
                'description' =>  '店主诚意推荐 品质商品',
            ]
        ];


        return $info->toArray();
    }
}